//
//  ContrailObjectLayer.m
//  Agility
//
//  Created by Dominique d'Argent on 27.04.11.
//  Copyright 2011 Nicky Nubbel. All rights reserved.
//

#import "ContrailObjectLayer.h"

@implementation ContrailObjectLayer

#define RADIUS 20.0f

//
// initWithSceneryObject:
//
// Overriden to draw a circle and to set backgorund color.
//
- (id)initWithSceneryObject:(SceneryObject *)newSceneryObject {
    self = [super initWithSceneryObject:newSceneryObject];
    
    if (self) {
        [CATransaction begin];
        [CATransaction setDisableActions:YES];
        self.bounds             = CGRectMake(0.0f, 0.0f, RADIUS, RADIUS);
        self.cornerRadius       = RADIUS / 2.0f;
        self.backgroundColor    = [UIColor redColor].CGColor;
        self.masksToBounds      = YES;
        [CATransaction commit];
    }
    
    return self;
}

//
// start
//
// Overridden to start animations.
//
- (void)start {
    [CATransaction begin];
    [CATransaction setAnimationDuration:sceneryObject.maxLifeTime];
    [CATransaction setCompletionBlock:^(void) {
        [self removeFromSuperlayer];
    }];
    self.backgroundColor = [UIColor yellowColor].CGColor;
    self.transform = CATransform3DMakeScale(0.5f, 0.5f, 1.0f);
    [CATransaction commit];
}

//
// actionForKey:
//
// Custom animations for some properties.
//
- (id<CAAction>)actionForKey:(NSString *)key {
    if ([key isEqualToString:@"backgroundColor"] ||
        [key isEqualToString:@"transform"]) {
        CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:key];
        anim.fromValue = [self valueForKey:key];
        
        return anim;
    }
    else {
        return [super actionForKey:key];
    }
}

@end